﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Configuration;
using Ivy.FeedbackModule.DataAccess;
using Ivy.UserRegistration.Entity;
using Ivy.FeedbackModule.Entity;
using Ivy.UserRegistration.DataAccess;
using Ivy.Common.Framework.EmailUtility;
using Ivy.FeedbackModule.IBusinessLogic;


namespace Ivy.FeedbackModule.BusinessLogic
{
    public class FeedbackModuleBus : IFeedbackModuleBus
    {
        #region Variables & Constants

        #endregion Variables & Constants

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Public Properties
        #endregion Public Properties

        #region Public Methods
        #region IFeedbackModuleBus Members
        public bool SubmitFeedBack(FeedbackUser feedbackUserEntity, Feedback feedbackEntity)
        {
            bool IsOperationSuccessful = false;
            try
            {
                FeedbackUserDao lFeedbackUserDao = new FeedbackUserDao();
                FeedbackDao lFeedbackDao = new FeedbackDao();
                FeedbackUser lFeedbackUser = lFeedbackUserDao.GetByEmail(feedbackUserEntity.FeedbackUserEmail);



                if (lFeedbackUser == null)
                {
                    feedbackUserEntity.RegisteredUserkey = GetUserKey(feedbackUserEntity.FeedbackUserEmail);
                    lFeedbackUserDao.Insert(feedbackUserEntity);
                    lFeedbackUser = lFeedbackUserDao.GetByEmail(feedbackUserEntity.FeedbackUserEmail);
                    feedbackEntity.FeedbackUserKey = lFeedbackUser.FeedbackUserKey;
                    //lFeedbackDao.Insert(feedbackEntity);
                    //IsOperationSuccessful = true;
                }
                else
                {
                    feedbackEntity.FeedbackUserKey = lFeedbackUser.FeedbackUserKey;
                    //lFeedbackDao.Insert(feedbackEntity);
                    //IsOperationSuccessful = true;
                }
                lFeedbackDao.Insert(feedbackEntity);
                IsOperationSuccessful = true;
                SendFeedbackNotificationEmail(lFeedbackUser.FeedbackUserName, lFeedbackUser.FeedbackUserEmail, feedbackEntity.FeedbackText);
                //if (!IsFeedbackUserEmailExisting(feedbackUserEntity))
                //{
                //    lFeedbackUserDao = new FeedbackUserDao();
                //    lFeedbackUserDao.Insert(feedbackUserEntity);

                //    lFeedbackDao = new FeedbackDao();
                //    feedbackEntity.FeedbackUserKey = feedbackUserEntity.FeedbackUserKey;
                //    feedbackEntity.Sys_DateOfCreation = feedbackUserEntity.Sys_DateOfCreation;

                //    lFeedbackDao.Insert(feedbackEntity);
                //}
                //else
                //{
                //    lFeedbackDao = new FeedbackDao();
                //    feedbackEntity.FeedbackUserKey = feedbackUserEntity.FeedbackUserKey;
                //    lFeedbackDao.Insert(feedbackEntity);

                //}
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);

            }
            return IsOperationSuccessful;
        }

        /// method for creating New FeedbackUser
        /// by ARMV
        ///
        public bool CreateFeedBackUser(FeedbackUser FeedbackUser)
        {
            bool IsOperationSuccessful = false;
            try
            {
                FeedbackUserDao lFeedbackUserDao = new FeedbackUserDao();
                lFeedbackUserDao.Insert(FeedbackUser);
                IsOperationSuccessful = true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return IsOperationSuccessful;
        }

        public bool IsFeedbackUserEmailExisting(FeedbackUser FeedbackUser)
        {
            bool IsFeedbackUserEmailExisting;
            try
            {
                FeedbackUserDao lFeedbackUserDao = new FeedbackUserDao();
                FeedbackUser lFeedbackUser;
                lFeedbackUser = lFeedbackUserDao.GetByEmail(FeedbackUser.FeedbackUserEmail);
                if (lFeedbackUser == null)
                {
                    IsFeedbackUserEmailExisting = false;
                }
                else
                {
                    IsFeedbackUserEmailExisting = true;
                    FeedbackUser.FeedbackUserKey = lFeedbackUser.FeedbackUserKey;
                }


            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return IsFeedbackUserEmailExisting;
        }

        /// method for Checking Existing FeedbackUser
        /// by ARMV
        ///
        public bool IsFeedbackUserExisting(string UserEmailID)
        {
            bool FeedbackUserExisting = true;
            try
            {
                FeedbackUserDao lFeedbackUserDao = new FeedbackUserDao();
                FeedbackUser lFeedbackUser;
                lFeedbackUser = lFeedbackUserDao.GetByEmail(UserEmailID);
                if (lFeedbackUser == null)
                {
                    FeedbackUserExisting = false;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return FeedbackUserExisting;
        }

        public int GetUserKey(string EmailID)
        {
            int Userkey = -1;
            try
            {
                UserDao lUserDao = new UserDao();

                UserCriteria lUserCriteria = new UserCriteria();
                lUserCriteria.UserPrimaryEmailID = EmailID;
                List<User> lListUsers = new List<User>();

                lListUsers = lUserDao.GetByCriteria(lUserCriteria);
                if (lListUsers.Count > 0)
                {
                    Userkey = lListUsers[0].UserKey;
                }
                else
                {
                    Userkey = 1;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return Userkey;
        }

        public bool SendFeedbackNotificationEmail(string feedbackUserName, string feedbackUserEmailId, string feedbackText)
        {
            bool IsOperationSuccessful = false;
            try
            {
                EmailUtility lEmailUtility = new EmailUtility();
                MailMessage lMailMsg = new MailMessage();
                lMailMsg.Subject = "Framework Ivy - Feedback received notification";
                lMailMsg.IsBodyHtml = true;
                lMailMsg.Body = @"<html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"">";
                lMailMsg.Body = lMailMsg.Body + @"<head> <style type=""text/css""> .style1 { color: #006600; } .style2 { text-align: left; font-size: small; } .style3 { font-size: large; } .style5 { font-size: large; font-weight: bold; color: #006600; } .style6 { text-align: left; font-size: small; color: #006600; } </style> </head>";
                lMailMsg.Body = lMailMsg.Body + @"<body style=""font-family: 'MS Sans Serif'"">";
                lMailMsg.Body = lMailMsg.Body + @"<table class=""style3"">";
                lMailMsg.Body = lMailMsg.Body + @"<tr> <td> <h1 class=""style1""> Feedback  Notification! </h1> </td> </tr>";
                lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> Framework Ivy has received a Feedback at the web site.</td> </tr>";
                lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> <span class=""style3"">User Name : </span><span class=""style5"">" + feedbackUserName + "</span></td> </tr>";
                lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> User Email : <span class=""style5"">" + feedbackUserEmailId + "</span></td> </tr>";
                lMailMsg.Body = lMailMsg.Body + @"<tr> <td> Feedback Details:</td> </tr>";
                
                lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style6"">" + feedbackText + "</td> </tr>";
                lMailMsg.Body = lMailMsg.Body + @"<tr> <td> &nbsp; </td> </tr>";
                lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Thanks and Regards </td> </tr>";
                lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Framework Ivy - Notification System</td> </tr>";
                lMailMsg.Body = lMailMsg.Body + @"</table></body></html>";

                lEmailUtility.AddEmailSenderAddressToMailMessage(lMailMsg, "frameworkivy@gmail.com", "Administrator FrameworkIvy");
                lEmailUtility.AddEmailRecepientOrReplyAddressesToMailMessage(lMailMsg, EmailAddressType.SendTo, "frameworkivy@googlegroups.com");

                lEmailUtility.SendEmail(lMailMsg);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return IsOperationSuccessful;
        }

        public List<Feedback> GetAllFeedback()
        {
            var lFeedbackList = new List<Feedback>();
            try
            {
                FeedbackDao IFeedbackDao = new FeedbackDao();
                lFeedbackList = IFeedbackDao.GetAll();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return lFeedbackList;
        }


        #endregion
        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        #endregion Private Methods
    }
}
